//
//  WDCancelWorkOrderTopView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/6.
//

#import "WDCancelWorkOrderTopView.h"
#import "WDArrivalServiceItemModel.h"


@interface WDCancelWorkOrderTopView ()

@property (nonatomic, strong) WDArrivalServiceItemModel *model;

@property (nonatomic, strong) UIView *topBgView;

/// 标题
@property (nonatomic, strong) UILabel *titleLabel;

/// 取消申请
@property (nonatomic, strong) UIButton *btnCancelApply;

@property (nonatomic, strong) UIView *midBgView;




@property (nonatomic, strong) CALayer *topLine;

/// 车牌号
@property (nonatomic, strong) UILabel *plateNumberLabel;

/// vin码后八位
@property (nonatomic, strong) UILabel *vinLabel;

/// 更新时间
@property (nonatomic, strong) UILabel *updateTimeLabel;

/// 车辆品牌图片
@property (nonatomic, strong) UIImageView *mainImgView;

/// 车系
@property (nonatomic, strong) UILabel *vehicleSalesLabel;

/// 车型
@property (nonatomic, strong) UILabel *modelNameLabel;

/// 工单号
@property (nonatomic, strong) UILabel *workOrderCodeLabel;

/// 预约时间
@property (nonatomic, strong) UILabel *appointDatetimeLabel;

/// 状态
@property (nonatomic, strong) UILabel *statusLabel;

@end

@implementation WDCancelWorkOrderTopView

- (void)initWithVar {
    [super initWithVar];
    
}

- (void)initWithSubView {
    [super initWithSubView];
    self.backgroundColor = [UIColor colorWithHexString:@"#F8F8F8"];
    
    [self initWithTopBgView];
    
    /// 标题
    [self initWithTitleLabel];

    /// 取消申请
//    [self initWithBtnCancelApply];
    
    [self initWithMidBgView];

    [self initWithTopLine];
}

- (void)initWithTopBgView {
    if (!_topBgView) {
        CGRect frame = CGRectMake(0, 0, self.width, 40);
        _topBgView = [[UIView alloc] initWithFrame:frame];
        _topBgView.backgroundColor = [UIColor colorWithHexString:@"#F8F8F8"];
        [self addSubview:_topBgView];
    }
}

/// 标题
- (void)initWithTitleLabel {
    if (!_titleLabel) {
        CGRect frame = CGRectMake(20, (self.topBgView.height - 20) * 0.5, (self.width - 40) * 0.5, 20);
        _titleLabel = [UILabel createWithLabel:self.topBgView frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#000000"];
            label.text = @"车辆基本信息";
        }];
    }
}

/// 取消申请
- (void)initWithBtnCancelApply {
    /*
    if (!_btnCancelApply) {
        CGRect frame = CGRectMake(self.width - 20 - 74, (self.topBgView.height - 28) * 0.5, 74, 28);
        _btnCancelApply = [UIButton createWithButton:self.topBgView frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] target:self action:@selector(actionWithCancelApply:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setTitle:@"申请取消" forState:UIControlStateNormal];
            [button setTitleColor:kThemeColor forState:UIControlStateNormal];
            button.backgroundColor = [UIColor whiteColor];
            button.layer.borderColor = kThemeColor.CGColor;
            button.layer.borderWidth = 1;
            button.layer.cornerRadius = 2;
            button.layer.masksToBounds = YES;
        }];
    }*/
}

- (void)initWithMidBgView {
    if (!_midBgView) {
        CGRect frame = CGRectMake(0, self.topBgView.bottom, self.width, 141);
        _midBgView = [[UIView alloc] initWithFrame:frame];
        _midBgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_midBgView];
    }
}

- (void)initWithTopLine {
    if (!_topLine) {
        CGRect frame = CGRectMake(20, 36, self.width - 20 * 2, 1);
        _topLine = [CALayer drawLine:self.midBgView frame:frame lineColor:[UIColor colorWithHexString:@"#EEEEEE"]];
    }
}

/// 车牌号
- (void)initWithPlateNumberLabel {
    if (!_plateNumberLabel) {
        NSString *str = @"皖A12345645";
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        float width = [str widthForFont:font];
        CGRect frame = CGRectMake(20, (self.topLine.top - 16) * 0.5, width, 16);
        _plateNumberLabel = [UILabel createWithLabel:self.midBgView frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
        }];
    }
    if (self.model) {
        NSString *str = objectNull(self.model.plateNumber);
        float width = [str widthForFont:_plateNumberLabel.font] + 2;
        self.plateNumberLabel.text = str;
        self.plateNumberLabel.width = width;
    }
}

- (void)initWithVinLabel {
    if (!_vinLabel) {
        CGRect frame = _plateNumberLabel.frame;
        frame.size.width = frame.size.width + 10;
        frame.origin.x = _plateNumberLabel.right + 5;
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        _vinLabel = [UILabel createWithLabel:self.midBgView frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
        }];
    }
    if (self.model) {
        NSString *str = objectNull(self.model.vehicleVin);
        if (self.model.vehicleVin && self.model.vehicleVin.length >= 8) {
            str = [self.model.vehicleVin substringFromIndex:self.model.vehicleVin.length - 8];
        }
        float width = [str widthForFont:_vinLabel.font] + 2;
        self.vinLabel.width = width;
        self.vinLabel.left = self.plateNumberLabel.right + 5;
        self.vinLabel.text = str;
    }
}

/// 更新时间
- (void)initWithUpdateTimeLabel {
    if (!_updateTimeLabel) {
        NSString *str = @"更新：2024-12-12 23:34:54";
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        float width = [str widthForFont:font] + 2;
        CGRect frame = self.plateNumberLabel.frame;
        frame.size.width = width;
        frame.origin.x = self.width - 20 - width;
        _updateTimeLabel = [UILabel createWithLabel:self.midBgView frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textAlignment = NSTextAlignmentRight;
        }];
    }
    if (self.model) {
        NSString *str = [NSString stringWithFormat:@"更新：%@", objectNull(self.model.updateTime)];
        float width = [str widthForFont:_updateTimeLabel.font] + 2;
        self.updateTimeLabel.width = width;
        self.updateTimeLabel.left = self.width - 20 - width;
        self.updateTimeLabel.text = str;
    }
}

- (void)initWithMainImgView {
    if (!_mainImgView) {
        CGRect frame = CGRectMake(20, _topLine.bottom + 19, 86, 68);
        _mainImgView = [[UIImageView alloc] initWithFrame:frame];
        [self.midBgView addSubview:_mainImgView];
    }
    if (self.model) {
        [_mainImgView sd_setImageWithURL:[NSURL URLWithString:self.model.imgPath] placeholderImage:[UIImage imageNamed:@"ic_default_veh"]];
    }
}

/// 状态
- (void)initWithStatusLabel {
    if (!_statusLabel) {
        NSString *str = @"待接车";
        UIFont *font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        float width = [str widthForFont:font] + 2;
        CGRect frame = CGRectMake(self.width - 20 - width, self.topLine.bottom + 9, width, 20);
        _statusLabel = [UILabel createWithLabel:self.midBgView frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#F9AD3B"];
            label.textAlignment = NSTextAlignmentRight;
        }];
    }
    if (self.model) {
        // 服务流程 0.待分派 1.待接车 2.检查中 3.维修中 4.待报单 5.已完成 6.已取消 7.已关闭
        NSString *str = [EDSUtility getServFlowName:self.model.servFlow];
        float width = [str widthForFont:self.statusLabel.font] + 2;
        self.statusLabel.width = width;
        self.statusLabel.left = self.width - 20 - width;
        self.statusLabel.text = str;
        
    }
}

/// 车系
- (void)initWithVehicleSalesLabel {
    if (!_vehicleSalesLabel) {
        CGRect frame = CGRectMake(self.mainImgView.right + 12, 0, self.statusLabel.left - self.mainImgView.right - 12 - 10, 20);
        _vehicleSalesLabel = [UILabel createWithLabel:self.midBgView frame:frame font:[UIFont systemFontOfSize:16 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#000000"];
//            label.backgroundColor = [UIColor orangeColor];
        }];
    }
    if (self.model) {
        self.vehicleSalesLabel.centerY = self.statusLabel.centerY;
        self.vehicleSalesLabel.text = objectNull(self.model.vehicleSalesValue);
    }
}

/// 车型
- (void)initWithModelNameLabel {
    if (!_modelNameLabel) {
        CGRect frame = self.vehicleSalesLabel.frame;
        frame.size.height = 18;
        frame.origin.y = self.vehicleSalesLabel.bottom + 1;
        frame.size.width = self.width - self.mainImgView.right - 20 - 12;
        _modelNameLabel = [UILabel createWithLabel:self.midBgView frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#19C46E"];
//            label.backgroundColor = [UIColor orangeColor];
        }];
    }
    if (self.model) {
        self.modelNameLabel.text = objectNull(self.model.modelName);
    }
}



/// 工单号
- (void)initWithWorkOrderCodeLabel {
    if (!_workOrderCodeLabel) {
//        NSString *str = @"工单号：ABCDQWEQ";
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
//        float width = [str widthForFont:font] + 5;
        CGRect frame = self.modelNameLabel.frame;
        frame.size.height = 18;
        frame.origin.y = self.modelNameLabel.bottom + 1;
        _workOrderCodeLabel = [UILabel createWithLabel:self.midBgView frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#888888"];
        }];
    }
    if (self.model) {
        self.workOrderCodeLabel.text = [NSString stringWithFormat:@"工单号：%@", self.model.taskNo];
    }
}

/// 预约时间
- (void)initWithAppointDatetimeLabel {
    if (!_appointDatetimeLabel) {
//        NSString *str = @"预约日期：2024-05-12 12:34";
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        CGRect frame = self.workOrderCodeLabel.frame;
        frame.origin.y = self.workOrderCodeLabel.bottom + 2;
        _appointDatetimeLabel = [UILabel createWithLabel:self.midBgView frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#888888"];
        }];
    }
    if (self.model) {
        NSString *str = objectNull(self.model.appointmentTime);
        if ([str isEqualToString:@""]) {
            str = objectNull(self.model.createTime);
        }
        self.appointDatetimeLabel.text = [NSString stringWithFormat:@"预约日期：%@", str];
//        self.appointDatetimeLabel.text = [NSString stringWithFormat:@"预约日期：%@", self.model.appointmentTime];
    }
}

- (void)updateWithViewData:(id)data {
    self.model = data;
    
    [self initWithPlateNumberLabel];
    
    [self initWithVinLabel];

    [self initWithUpdateTimeLabel];

    [self initWithMainImgView];
    
    [self initWithStatusLabel];

    /// 车系
    [self initWithVehicleSalesLabel];

    /// 车型
    [self initWithModelNameLabel];

    /// 工单号
    [self initWithWorkOrderCodeLabel];
//
    /// 预约时间
    [self initWithAppointDatetimeLabel];
    
}

@end
